package jbox2dprocessing.compontent;
import org.jbox2d.collision.PolygonDef;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import pbox2d.PBox2D;

import processing.core.PApplet;
import processing.core.PConstants;

public class BaseBox extends AbstractBox2DShape{
		
	//	BaseBox Shape
	public float baseWidth;
	public float baseHeight;
	
	//	Define Basic BaseBox
	public BaseBox(PBox2D _box2d, int _master){
		super(_box2d);
		this.baseWidth = 40.0f;
		this.baseHeight = 15.0f;
		this.master = _master;
		makeBaseBody(baseWidth, baseHeight, new Vec2(baseX, baseY));
	}
	
	public BaseBox(PBox2D _box2d, int _master, float baseDensity){
		super(_box2d, baseDensity);
		this.baseWidth = 40.0f;
		this.baseHeight = 15.0f;
		this.master = _master;
		makeBaseBody(baseWidth, baseHeight, new Vec2(baseX, baseY));
	}
	
	//	Define costumized BaseBox
	public BaseBox(float baseX, float baseY, float baseWidth,
			float baseHeight, PBox2D _box2d, int _master, float baseDensity, float baseFriction,
			float baseRestitution) {
		super(baseX, baseY, _box2d, baseDensity, baseFriction, baseRestitution);
		this.baseWidth = baseWidth;
		this.baseHeight = baseHeight;
		this.master = _master;
		makeBaseBody(baseWidth, baseHeight, new Vec2(baseX, baseY));
	}
	
	
	public BaseBox(float baseX, float baseY, float baseWidth,
			float baseHeight, PBox2D _box2d, int _master, int baseColor, int baseStrokeColor,
			int baseStrokeWeight, float baseDensity, float baseFriction,
			float baseRestitution) {
		super(baseX, baseY, _box2d, baseColor, baseStrokeColor, baseStrokeWeight, baseDensity, baseFriction, baseRestitution);
		this.baseWidth = baseWidth;
		this.baseHeight = baseHeight;
		this.master = _master;
		makeBaseBody(baseWidth, baseHeight, new Vec2(baseX, baseY));
	}
	
	public void makeBaseBody(float _w, float _h, Vec2 center){
		PolygonDef sd = new PolygonDef();
		float box2dW = box2d.scaleScreenToWorld(_w/2);
		float box2dH = box2d.scaleScreenToWorld(_h/2);
		sd.setAsBox(box2dW, box2dH);
		
		sd.density = baseDensity;
		sd.friction = baseFriction;
		sd.restitution = baseRestitution;
		
		BodyDef bd = new BodyDef(this.master);
		bd.position.set(box2d.screenToWorld(center));
		
		baseBody = box2d.createBody(bd);
		baseBody.createShape(sd);
		baseBody.setMassFromShapes();
	}
	
	public void display(){
		Vec2 pos = box2d.getScreenPos(baseBody);
		float a = baseBody.getAngle();
		
		parent.rectMode(PConstants.CENTER);
		parent.pushMatrix();
		parent.translate(pos.x, pos.y);
		parent.rotate(PConstants.PI - a);
		parent.fill(baseColor);
		parent.stroke(baseStrokeColor);
		parent.strokeWeight(baseStrokeWeight);
		parent.rect(0, 0, baseWidth, baseHeight);
		parent.popMatrix();
	}
	
}
